<?

# 
# Copyright (c) 2002-2007 Eric Wallengren
# This file is part of the Continuous Automated Build and Integration 
# Environment (CABIE)
# 
# CABIE is distributed under the terms of the GNU General Public
# License version 2 or any later version.  See the file COPYING for copying 
# permission or http://www.gnu.org. 
#                                                                            
# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR  
# IMPLIED, without even the implied warranty of MERCHANTABILITY or FITNESS 
# FOR A PARTICULAR PURPOSE.  ANY USE IS AT YOUR OWN RISK. 
#                                                                            
# Permission to modify the code and to distribute modified code is granted, 
# provided the above notices are retained, and a notice that the code was 
# modified is included with the above copyright notice. 
# 

    class sqlfuncs {

        function __construct($server, $username, $password, $database) {

            $this->server   = $server;
            $this->username = $username;
            $this->password = $password;
            $this->database = $database;

        }

        function connect() {
  
            mysql_connect($this->server, $this->username, $this->password);
            mysql_select_db($this->database);

        }

        function sql_update($query) {

            $a      = 0;
            $return = array();

            if (strlen(stristr($query, "delete"))) {
                $return[$a] = "Cannot delete from update function";
                return $return;
            }

            $this->connect();

            $result=mysql_query($query);

            mysql_close();

            return $return;

        }

        function sql_query($query, $sep) {

            $a      = 0;
            $return = array();

            if (strlen(stristr($query, "delete"))) {
                $return[$a] = "Cannot delete from query function";
                return $return;
            }

            $this->connect();

            $result=mysql_query($query);

            while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $length = count($line);
                $entry = implode("$sep", $line);
                $return[$a] = $entry;
                $a++;
            }

            mysql_close();

            return $return;

        }

        function sql_delete($query) {

            $a      = 0;
            $return = array();

            if (strlen(stristr($query, "select"))) {
                $return[$a] = "Cannot query from delete function";
                return $return;
            }

            $this->connect();

            $result=mysql_query($query);

            mysql_close();

        }

        function sql_submit($sqltable, $record, $tabledefs) {

            $this->connect();

            $recsize = sizeof($record);

            $counter = 0;

            $tablevals = array();

            foreach ($tabledefs as $key => $val) {
                $tablevals[$counter++] = $val;
            }
            
            $counter = 1;

            foreach ($record as $value) {
                #
                # Since the verification password is sent, we ignore it
                #
                if ($tablevals[$counter-1]['md5'] && !$found) {
                    $val = md5($value);
                    $found = 1;
                } else {
                    $val = $value;
                }
                if ($counter == $recsize) {
                    $string .= "'$val'";
                } else {
                    $string .= "'$val',";
                }
                $counter++;
            }

            $query = "INSERT INTO ".$sqltable." VALUES (".$string.")";

            $result=mysql_query("$query");

            mysql_close();

            return $query;

        }

        function _get_hosttime() {

            $sqlquery = "select unix_timestamp()";

            $sqlarray = $this->sql_query($sqlquery, ";");

            return $sqlarray[0];

        }

        function __destruct(){}

    }

?>
